package wicket.example.components.filter.base;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.JavaScriptHeaderItem;
import org.apache.wicket.markup.html.TransparentWebMarkupContainer;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.request.resource.PackageResourceReference;

/**
 * 
 * @author ngolubev
 * 
 *         Filtering component for all table's
 * 
 */
public class TableFilter extends Panel
{

   /**
    * State for filter(e.g opened/closed)
    */
   private boolean             opened                  = Boolean.FALSE;

   private final RepeatingView filterComponentRepeater = new RepeatingView("filterComponent");

   /**
    * 
    * @param id
    *           - Wicket component id
    * @param filters
    *           - List of available filters
    */
   public TableFilter(final String id)
   {
      super(id);
      final TransparentWebMarkupContainer filterButtonImage = new TransparentWebMarkupContainer("filterButtonImage")
      {
         @Override
         protected void onComponentTag(ComponentTag tag)
         {
            super.onComponentTag(tag);
            String attributeValue = opened ? "" : "";
            tag.addBehavior(new AttributeModifier("class", attributeValue));
         }
      };
      filterButtonImage.setOutputMarkupId(true);
      final TransparentWebMarkupContainer filterListContainer = new TransparentWebMarkupContainer("filterListContainer")
      {
         @Override
         protected void onConfigure()
         {
            super.onConfigure();
            this.setVisible(opened);
         }
      };
      filterListContainer.setOutputMarkupId(true);
      filterListContainer.setVisibilityAllowed(true);
      filterListContainer.setOutputMarkupPlaceholderTag(true);

      filterListContainer.add(filterComponentRepeater);
      this.add(filterListContainer);

      final AjaxLink<?> filterButton = new AjaxLink<Void>("filterButton")
      {

         @Override
         public void onClick(AjaxRequestTarget target)
         {
            opened = !opened;
            target.add(filterButtonImage, filterListContainer);
         }
      };
      filterButton.add(filterButtonImage);
      this.add(filterButton);
   }

   public void addFilterEntry(AbstractFilterEntry filterEntry)
   {
      filterComponentRepeater.add(filterEntry);
   }

   public String newFilterEntryId()
   {
      return filterComponentRepeater.newChildId();
   }

   @Override
   public void renderHead(IHeaderResponse response)
   {
      super.renderHead(response);
      response.render(JavaScriptHeaderItem
            .forReference(new PackageResourceReference(TableFilter.class, "../resources/js/jquery.blockUI.js")));
   }

}
